import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shared_components/build-bottom-sheet/buildBottomSheetWidget.dart';
import 'package:shared_components/uploadImage.dart';
import 'package:shared_components/utils/global_prefs.dart';

void chooseImage(
    {Function? onSuccess,
    Function? onOcrContent,
    String imageType = '',
    String type = "All",
    bool multiple = false}) async {
  if (type == "Camera") {
    openPickImage(
        onSuccess: onSuccess,
        onOcrContent: onOcrContent,
        imageType: imageType,
        source: ImageSource.camera);
  }
  if (type == "Gallery") {
    openPickImage(
        onSuccess: onSuccess,
        onOcrContent: onOcrContent,
        imageType: imageType,
        source: ImageSource.gallery,
        multiple: multiple);
  }
  if (type == "All") {
    showModalBottomSheet(
        builder: (BuildContext context) {
          //构建弹框中的内容
          return buildBottomSheetWidget(
              itemList: [
                {
                  "label": "拍照",
                  "onClick": () {
                    print("点击第一个");
                    openPickImage(
                        onSuccess: onSuccess,
                        onOcrContent: onOcrContent,
                        imageType: imageType,
                        source: ImageSource.camera);
                  }
                },
                {
                  "label": "我的相册",
                  "onClick": () {
                    openPickImage(
                        onSuccess: onSuccess,
                        onOcrContent: onOcrContent,
                        imageType: imageType,
                        source: ImageSource.gallery,
                        multiple: multiple);
                  }
                }
              ],
              onSelect: (code) {
                if (code == "Camera") {
                  openPickImage(
                      onSuccess: onSuccess,
                      onOcrContent: onOcrContent,
                      imageType: imageType,
                      source: ImageSource.camera);
                }
                if (code == "Gallery") {
                  openPickImage(
                      onSuccess: onSuccess,
                      onOcrContent: onOcrContent,
                      imageType: imageType,
                      source: ImageSource.gallery,
                      multiple: multiple);
                }
              });
        },
        backgroundColor: Colors.transparent, //重要
        context: Get.overlayContext!);
  }
}

openPickImage(
    {Function? onSuccess,
    Function? onOcrContent,
    String imageType = '',
    ImageSource source = ImageSource.gallery,
    bool multiple = false}) async {
  var showPhotoPremiss = GlobalPrefs.getValue("showPhotoPremiss");
  var showCaneraPremiss = GlobalPrefs.getValue("showCaneraPremiss");
  if (showPhotoPremiss != 'false' && source == ImageSource.gallery) {
    // Get.back();
    //延迟1秒执
    Future.delayed(Duration(seconds: 1), () {
      // 向用户说明权限用途
      showDialog(
        context: Get.overlayContext!,
        builder: (context) => AlertDialog(
          title: Text('相册权限请求'),
          content: Text('我们需要访问您的相册以便您能上传照片。'),
          actions: [
            TextButton(
              onPressed: () {
                Get.back();
                // Navigator.pop(context);
                GlobalPrefs.saveValue("showPhotoPremiss", 'false');
                openPickImage(
                    onSuccess: onSuccess, source: source, multiple: multiple);
              },
              child: Text('允许'),
            ),
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text('拒绝'),
            ),
          ],
        ),
      );
    });
    return;
  }
  if (showCaneraPremiss != 'false' && source == ImageSource.camera) {
    // Get.back();
    //延迟1秒执
    Future.delayed(Duration(seconds: 1), () {
      // 向用户说明权限用途
      showDialog(
        context: Get.overlayContext!,
        builder: (context) => AlertDialog(
          title: Text('相机权限请求'),
          content: Text('我们需要访问您的相机以便您能上传照片。'),
          actions: [
            TextButton(
              onPressed: () {
                Get.back();
                // Navigator.pop(context);
                GlobalPrefs.saveValue("showCaneraPremiss", 'false');
                openPickImage(
                    onSuccess: onSuccess, source: source, multiple: multiple);
              },
              child: Text('允许'),
            ),
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text('拒绝'),
            ),
          ],
        ),
      );
    });
    return;
  }
  final ImagePicker _picker = ImagePicker();

  if (multiple && source == ImageSource.gallery) {
    // XFile? image =
    List<XFile>? images = await _picker.pickMultiImage(imageQuality: 20);
    print("选择的的图片");
    print(images);
    images.forEach((element) {
      print(element);
      if (element != null) {
        uploadImage(originalImage: element, onSuccess: onSuccess);
      }
    });
    // await _picker.pickImage(source: ImageSource.gallery, imageQuality: 1);
    // if (image != null) {
    // uploadImage(originalImage: image, onSuccess: onSuccess);
    // loadingShow(timeOut: 180, msg: "正在上传");
    // cropImage(image!);
    // }
  }
  if (!multiple) {
    XFile? image = await _picker.pickImage(source: source, imageQuality: 20);
    // await _picker.pickImage(source: ImageSource.gallery, imageQuality: 1);
    if (image != null) {
      uploadImage(originalImage: image, onSuccess: onSuccess);
      // loadingShow(timeOut: 180, msg: "正在上传");
      // cropImage(image!);
    }
  }

  // List<File?> images = await FlutterImagesPicker.pickImages(maxImages: 5);

  // List<Asset> images = await MultiImagePicker.pickImages(
  //   maxImages: 4, //最多选择4张照片
  //   enableCamera: true,
  //   selectedAssets: _images,
  //   cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
  //   materialOptions: MaterialOptions(
  //     actionBarColor: "#abcdef",
  //     actionBarTitle: "选择照片",
  //     allViewTitle: "所有照片",
  //     useDetailsView: false,
  //     selectCircleStrokeColor: "#000000",
  //   ),
  // );
}
